﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Housing;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

public partial class SearchControl : System.Web.UI.UserControl
{
    Admissibility admissibility = new Admissibility();
    Apply apply = new Apply();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && Session["UserId"] != null)
        {
            PageBind();
            BindDate();
            StreetBind();
        }
    }
    #region 街道选择事件
    protected void StreetBind()
    {
        if (Session["UserStreet"].ToString() == "海曙")
        {
            this.td_street.Style.Add("display", "");
        }
    }

    //街道选择的事件
    protected void ddlstreet_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet ds = admissibility.GetAdmissibilityForStreetKey(this.ddlstreet.SelectedValue);
        lblPageInfo.Text = ds.Tables[0].Rows.Count.ToString();
        gvstreetshow.DataSource = ds;
        gvstreetshow.DataBind();
        GetTotalCount(ds.Tables[0].Rows.Count);
    }
    #endregion

    protected void BindDate()
    {
        this.ddlMonth.Items.Clear();
        this.ddlMonth.Items.Add("全部");
        for (int i = 1; i <= 12; i++)
        {

            if (i < 10)
                this.ddlMonth.Items.Add("0" + i.ToString());
            else
                this.ddlMonth.Items.Add(i.ToString());
        }
        this.ddlMonth.SelectedIndex = 1;

    }


    public bool IsDateTime(string source)
    {
        try
        {
            DateTime time = Convert.ToDateTime(source);
            return true;
        }
        catch
        {
            return false;
        }
    }

    protected void PageBind()
    {
        DataSet ds = admissibility.GetAdmissibilityForCheck(Session["UserStreet"].ToString(), "200901"); //默认查询2009年01月的数据
        lblPageInfo.Text = ds.Tables[0].Rows.Count.ToString();
        gvstreetshow.DataSource = ds;
        gvstreetshow.DataBind();
        GetTotalCount(ds.Tables[0].Rows.Count);
        DataSet ds1 = admissibility.GetAdmissibilityUnCommit(Session["UserStreet"].ToString());
        gvadmissibilityshow.DataSource = ds1;
        gvadmissibilityshow.DataBind();
    }
    protected void gvstreetshow_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "del")
        {
            int succ = admissibility.DeleteAdmissibility(Convert.ToInt32(e.CommandArgument.ToString()));
            if (succ > 0)
                PageBind();
            else
                WebHelper.Alert("删除失败，请重新操作！");
        }
        if (e.CommandName.ToLower() == "commit")
        {
            string id = e.CommandArgument.ToString();
            apply.GetApplyById(id);
            apply.apply_passed = 2;
            int succ = apply.UpdateAApplyByApplyId(apply);
            if (succ > 0)
                PageBind();
            else
                WebHelper.Alert("网络延时，请重新操作");
        } if (e.CommandName.ToLower() == "quickcommit")
        {
            string id = e.CommandArgument.ToString();
            string a = "";
            admissibility.SetApplypassed(id);
            int succ = Apply.Update_apply_checkstate_ByApply_id(id, "5");
            int succ2 = Apply.UpdateApply_passedByApply_noid(id, "2");
            if (succ > 0 && succ2 > 0)
                PageBind();
            else
                WebHelper.Alert("网络延时，请重新操作");
        }
    }
    protected void gvstreetshow_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        LinkButton del = (LinkButton)e.Row.FindControl("lkbtndel");
        if (del != null)
            del.Attributes.Add("onclick", "return confirm('您确认要删除此受理单吗？');");

    }
    protected bool AllowView(int noid, object applyid)
    {
        if (applyid == DBNull.Value)
            return false;
        else
        {
            Apply apply = new Apply(applyid.ToString());
            if (apply.apply_passed == 1)
                return true;
            else
                return false;
        }
    }
    protected void gvadmissibilityshow_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "pass")
        {
            if (admissibility.SetApplypassed(e.CommandArgument.ToString()))
                PageBind();
            else
                WebHelper.Alert("网络延时，请重新操作！");
        }
        if (e.CommandName.ToLower() == "unpass")
        {
            if (admissibility.SetApplyUnpass(e.CommandArgument.ToString()))
                PageBind();
            else
                WebHelper.Alert("网络延时，请重新操作！");
        }
    }
    protected void gvadmissibilityshow_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        LinkButton lbtnpass = (LinkButton)e.Row.FindControl("lkbtnpass");
        if (lbtnpass != null)
            lbtnpass.Attributes.Add("onclick", "return confirm('您确认房产核查通过？');");
        LinkButton lbtnunpass = (LinkButton)e.Row.FindControl("lkbtnunpass");
        if (lbtnunpass != null)
            lbtnunpass.Attributes.Add("onclick", "return confirm('确认房产核查不通过？');");
    }
    protected void gvstreetshow_PageIndexChanged(object sender, EventArgs e)
    {

    }

    //月份选择事件
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        string year = this.Select1.Items[this.Select1.SelectedIndex].Value;
        string mon = this.ddlMonth.SelectedValue;
        string ym = year + mon;
        DataSet ds;
        if (mon == "全部")
        { ds = admissibility.GetAdmissibilityForCheck(Session["UserStreet"].ToString()); }
        else
        {
            ds = admissibility.GetAdmissibilityForCheck(Session["UserStreet"].ToString(), ym);
        }

        gvstreetshow.DataSource = ds;
        lblPageInfo.Text = ds.Tables[0].Rows.Count.ToString();
        gvstreetshow.DataBind();
        GetTotalCount(ds.Tables[0].Rows.Count);
        this.txbcontent.Text = string.Empty;
    }

    //生成事件
    protected void Button1_Click(object sender, EventArgs e)
    {
        string year = this.Select1.Items[this.Select1.SelectedIndex].Value;
        string mon = this.ddlMonth.SelectedValue;
        if (mon == "全部" || mon == "01")
        {
            WebHelper.Alert("请选择2-12月份");
            return;
        }
        string ym = year + mon;

        SqlParameter[] param = new SqlParameter[1];
        param[0] = new SqlParameter("@YearMonth", int.Parse(ym));
        if (SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.StoredProcedure, "BuildDataByYearMonth", param) > 1)
            WebHelper.AlertAndRefresh("生成成功");
        else

            WebHelper.AlertAndRefresh("生成失败");

    }


    #region 搜索功能
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        BindGridview();
    }

    protected void BindGridview()
    {
        string year = this.Select1.Items[this.Select1.SelectedIndex].Value;
        string mon = this.ddlMonth.SelectedValue;
        string ym = year + mon;
        DataSet ds = null;
        if (string.IsNullOrEmpty(txbcontent.Text))
        {
            ds = admissibility.GetAdmissibilityByKey(Session["UserStreet"].ToString(), ym, string.Empty, -1);
        }
        else
        {
            if (ddlsearch.SelectedValue == "编号")
            {
                ds = admissibility.GetAdmissibilityByKey(Session["UserStreet"].ToString(), ym, txbcontent.Text, 0);
            }
            else
            {
                ds = admissibility.GetAdmissibilityByKey(Session["UserStreet"].ToString(), ym, txbcontent.Text, 1);
            }
        }
        gvstreetshow.DataSource = ds;
        gvstreetshow.DataBind();
        if (null != ds)
        {
            lblPageInfo.Text = ds.Tables[0].Rows.Count.ToString();
            GetTotalCount(ds.Tables[0].Rows.Count);
        }
    }
    #endregion

    #region gridView 分页

    //获取总记录数
    public void GetTotalCount(int rowsCount)
    {
        GridViewRow pagerRow = this.gvstreetshow.BottomPagerRow;
        if (null != pagerRow)
        {
            Label lblTotalCount = pagerRow.FindControl("lbl_TotalCount") as Label;   // refer to the TextBox with the NewPageIndex value
            lblTotalCount.Text = rowsCount + "";

        }
    }

    protected void gvstreetshow_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView theGrid = sender as GridView; // refer to the GridView
        int newPageIndex = 0;
        if (-2 == e.NewPageIndex)
        {
            TextBox txtNewPageIndex = null;
            //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
            GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
            if (null != pagerRow)
            {
                txtNewPageIndex = pagerRow.FindControl("Tb_page") as TextBox;   // refer to the TextBox with the NewPageIndex value
            }
            if (null != txtNewPageIndex)
            {
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
            }
        }
        else
        { // when click the first, last, previous and next Button
            newPageIndex = e.NewPageIndex;
        }
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
        theGrid.PageIndex = newPageIndex;

        if (string.IsNullOrEmpty(this.txbcontent.Text))
        {
            PageBind();
        }
        else
        {
            BindGridview();
        }
    }
    #endregion
}
